package View.BreakRule;

import Service.BreakRuleService;
import Service.ServiceImpl.BreakRuleServiceImpl;
import Service.ServiceImpl.UserServiceImpl;
import Service.UserService;
import View.Index.Index_admin;
import domain.BreakRule;
import domain.User;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;

public class QueryBreakRule extends JFrame {
    public QueryBreakRule(User user){
        init();
        componentInit(user);
    }
    public void init(){
        this.setTitle("查询学生违纪记录");
        this.setSize(933,518);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
    }
    public void componentInit(User user) {
        //------------------JLabel--------------------
        JLabel label1 = new JLabel();
        label1.setText("id:");
        label1.setBounds(40, 18, 65, 30);
        JLabel label2 = new JLabel();
        label2.setText("学号:");
        label2.setBounds(222, 18, 65, 30);
        JLabel label3 = new JLabel();
        label3.setText("姓名:");
        label3.setBounds(422, 19, 65, 30);
        JLabel label4 = new JLabel();
        label4.setText("违纪原因:");
        label4.setBounds(623, 20, 65, 30);

        //------------------JTextField--------------------
        JTextField field1 = new JTextField();
        field1.setText("");
        field1.setBorder(new LineBorder(Color.lightGray));
        field1.setBounds(90, 18, 120, 30);
        JTextField field2 = new JTextField();
        field2.setText("");
        field2.setBorder(new LineBorder(Color.lightGray));
        field2.setBounds(293, 19, 120, 30);
        JTextField field3 = new JTextField();
        field3.setText("");
        field3.setBorder(new LineBorder(Color.lightGray));
        field3.setBounds(493, 19, 120, 30);
        JTextField field4 = new JTextField();
        field4.setText("");
        field4.setBorder(new LineBorder(Color.lightGray));
        field4.setBounds(699, 20, 120, 30);

        //------------------JButton--------------------
        JButton button1 = new JButton();
        button1.setText("查询");
        button1.setBounds(200, 67, 175, 43);
        JButton button2 = new JButton();
        button2.setText("退出");
        button2.setBounds(500, 67, 175, 43);

        JScrollPane panel = new JScrollPane();
        panel.setBounds(100, 120, 700, 350);


        this.add(label1);
        this.add(label2);
        this.add(label3);
        this.add(label4);
        this.add(field1);
        this.add(field2);
        this.add(field3);
        this.add(field4);
        this.add(button1);
        this.add(button2);
        this.add(panel);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //存放表格的字段
                String title[] = {"id","学号","姓名","违纪原因","登记时间"};
                if(!"".equals(field1) && !"".equals(field2) && !"".equals(field3) && !"".equals(field4)) {
                    HashMap<String, String> map = new HashMap<>();
                    map.put("id", field1.getText());
                    map.put("stunum", field2.getText());
                    map.put("name", field3.getText());
                    map.put("disReason", field4.getText());

                    BreakRuleService service = new BreakRuleServiceImpl();
                    List<BreakRule> breakRules = service.fuzzySelectBreakRule(map);


                    //存放表格的内容
                    Object[][] neirong = new Object[breakRules.size()][5];
                    for (int i = 0; i < breakRules.size(); i++) {
                        BreakRule rule = breakRules.get(i);
                        neirong[i][0] = rule.getId();
                        neirong[i][1] = rule.getStunum();
                        neirong[i][2] = rule.getName();
                        neirong[i][3] = rule.getDisReason();
                        neirong[i][4] = rule.getWriteTime();
                    }

                    //查询结果如下
                    JTable table = new JTable(neirong, title);
                    DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
                    tcr.setHorizontalAlignment(SwingConstants.CENTER);
                    table.setDefaultRenderer(Object.class, tcr);
                    panel.setViewportView(table);
                }else {
                    //如果文本框什么都没有输入就直接把表中所有的内容展示
                    BreakRuleService service = new BreakRuleServiceImpl();
                    List<BreakRule> breakRules = service.breakRuleList();

                    //存放表格的内容
                    Object[][] neirong = new Object[breakRules.size()][5];
                    for (int i = 0; i < breakRules.size(); i++) {
                        BreakRule rule = breakRules.get(i);
                        neirong[i][0] = rule.getId();
                        neirong[i][1] = rule.getStunum();
                        neirong[i][2] = rule.getName();
                        neirong[i][3] = rule.getDisReason();
                        neirong[i][4] = rule.getWriteTime();
                    }

                    JTable table = new JTable(neirong, title);
                    DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
                    tcr.setHorizontalAlignment(SwingConstants.CENTER);
                    table.setDefaultRenderer(Object.class, tcr);
                    panel.setViewportView(table);
                }
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new Index_admin(user).setVisible(true);
            }
        });
    }
}
